/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.click.app.webapp.action;

import com.click.app.DateFormatChanger;
import com.click.app.model.BalanceLaundry;
import com.click.app.model.Coin;
import com.click.app.model.Laundry;
import com.click.app.model.LaundryRequest;
import com.click.app.model.LaundryStorage;
import com.click.app.model.PeriodPaymentLaundry;
import com.click.app.model.SizeRank;
import com.click.app.model.TradeMark;
import com.click.app.model.User;
import com.click.app.service.BalanceLaundryManager;
import com.click.app.service.GenericManager;
import com.click.app.service.LaundryRequestItemManager;
import com.click.app.service.LaundryRequestManager;
import com.click.app.service.PeriodPaymentLaundryManager;
import com.click.app.service.SeamStorageManager;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;

/**
 * @author Joel Chuca Condo
 */
public class LaundryRequestAction extends BaseAction {

    private LaundryRequestManager laundryRequestManager;
    private LaundryRequestItemManager laundryRequestItemManager;
    private GenericManager<Coin, Long> coinManager;
    private GenericManager<LaundryStorage, Long> laundryStorageManager;
    private GenericManager<Laundry, Long> laundryManager;
    private GenericManager<SizeRank, Long> sizeRankManager;
    private GenericManager<TradeMark, Long> tradeMarkManager;
    private PeriodPaymentLaundryManager periodPaymentLaundryManager;
    private SeamStorageManager seamStorageManager;
    private LaundryRequest laundryRequest;
    private List laundryRequests;
    private List laundries;
    private List coins;
    private List sizeRanks;
    private List tradeMarks;
    private Long id;
    private Long idLaundryRequest;
    private boolean withDiscount;
    private String dateLaundry;
    //private String receivedDate;
    private Date todayDate;
    //DateFormatChanger changer = new DateFormatChanger();
    private BalanceLaundryManager balanceLaundryManager;

    public void setCoinManager(GenericManager<Coin, Long> coinManager) {
        this.coinManager = coinManager;
    }

    public void setLaundryManager(GenericManager<Laundry, Long> laundryManager) {
        this.laundryManager = laundryManager;
    }

    public void setTradeMarkManager(GenericManager<TradeMark, Long> tradeMarkManager) {
        this.tradeMarkManager = tradeMarkManager;
    }

    public void setLaundryRequestItemManager(LaundryRequestItemManager laundryRequestItemManager) {
        this.laundryRequestItemManager = laundryRequestItemManager;
    }

    public void setLaundryRequestManager(LaundryRequestManager laundryRequestManager) {
        this.laundryRequestManager = laundryRequestManager;
    }

    public void setLaundryStorageManager(GenericManager<LaundryStorage, Long> laundryStorageManager) {
        this.laundryStorageManager = laundryStorageManager;
    }

    public void setSizeRankManager(GenericManager<SizeRank, Long> sizeRankManager) {
        this.sizeRankManager = sizeRankManager;
    }

    public void setPeriodPaymentLaundryManager(PeriodPaymentLaundryManager periodPaymentLaundryManager) {
        this.periodPaymentLaundryManager = periodPaymentLaundryManager;
    }

    public void setSeamStorageManager(SeamStorageManager seamStorageManager) {
        this.seamStorageManager = seamStorageManager;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getIdLaundryRequest() {
        return idLaundryRequest;
    }

    public void setIdLaundryRequest(Long idLaundryRequest) {
        this.idLaundryRequest = idLaundryRequest;
    }

    public LaundryRequest getLaundryRequest() {
        return laundryRequest;
    }

    public void setLaundryRequest(LaundryRequest laundryRequest) {
        this.laundryRequest = laundryRequest;
    }

    public List getLaundryRequests() {
        return laundryRequests;
    }

    public List getCoins() {
        return coins;
    }

    public List getLaundries() {
        return laundries;
    }

    public List getSizeRanks() {
        return sizeRanks;
    }

    public List getTradeMarks() {
        return tradeMarks;
    }

    public boolean isWithDiscount() {
        return withDiscount;
    }

    public void setWithDiscount(boolean withDiscount) {
        this.withDiscount = withDiscount;
    }

    public String getDateLaundry() {
        dateLaundry = DateFormatChanger.changeDatetoString(laundryRequest.getDate());
        return dateLaundry;
    }

    public void setDateLaundry(String dateLaundry) {
        this.dateLaundry = dateLaundry;
        laundryRequest.setDate(DateFormatChanger.changeStringtoDate(dateLaundry));
    }

    public Date getTodayDate() {
        return todayDate;
    }

    public void setTodayDate(Date todayDate) {
        this.todayDate = todayDate;
    }

    public String list() {
        laundryRequests = laundryRequestManager.getCurrentPeriodUnReception();
        return SUCCESS;
    }

    public String edit() {
        System.out.println("LaundryRequestAction -> edit()");
        laundries = laundryManager.getAll();
        coins = coinManager.getAll();
        tradeMarks = tradeMarkManager.getAll();
        sizeRanks = sizeRankManager.getAll();

        if (id != null) {
            laundryRequest = laundryRequestManager.get(id);
            
        } else {
            laundryRequest = new LaundryRequest();
            laundryRequest.setCoin(new Coin());            
            laundryRequest.setDate(new Date());

        }
        System.out.println("LaundryRequestAction -> end edit()");
        return SUCCESS;
    }

    public String save() throws Exception {
        System.out.println("LaundryRequestAction -> save() ");

        if (cancel != null) {
            return CANCEL;
        }

        boolean isNew = false;
        isNew = (laundryRequest.getId() == null);

        PeriodPaymentLaundry ppl= null;

        if (isNew) {

            ppl = periodPaymentLaundryManager.findCurrentPeriodByLaundry(laundryRequest.getLaundry().getId());
            laundryRequest.setPeriodPaymentLaundry(ppl);
            laundryRequest.setFailTotalPrice(new Double(0));
            laundryRequest.setClothesTotalPrice(0D);
            laundryRequest.setCurrentTotalPrice(0D);
            laundryRequest.setTotalPrice(new Double(0));
            laundryRequest.setQuantity(0);
            laundryRequest.setPayed(false);


        } else {
            LaundryRequest laundryRequesteOld = laundryRequestManager.get(laundryRequest.getId());
            laundryRequest.setTotalPrice(laundryRequesteOld.getTotalPrice());
            laundryRequest.setFailTotalPrice(laundryRequesteOld.getFailTotalPrice());
            laundryRequest.setPeriodPaymentLaundry(laundryRequesteOld.getPeriodPaymentLaundry());
            laundryRequest.setClothesTotalPrice(laundryRequest.getClothesTotalPrice());
            laundryRequest.setCurrentTotalPrice(laundryRequest.getCurrentTotalPrice());
        }

        laundryRequest.setUser(getUser());
        laundryRequest.setDate(DateFormatChanger.changeStringtoDate(dateLaundry));
        laundryRequest = laundryRequestManager.save(laundryRequest);

        if(isNew){
            BalanceLaundry balanceLaundry =  new BalanceLaundry();
            balanceLaundry.setLaundryRequest(laundryRequest);
            balanceLaundry.setDateBalance(laundryRequest.getDate());
            balanceLaundry.setAmountPaid(0D);
            balanceLaundry.setAmountWon(0D);
            balanceLaundry.setBalance(0D);
            balanceLaundry.setPeriodPaymentLaundry(ppl);
            balanceLaundry.setObservation("");
            balanceLaundry.setTypeBalance(1);
            balanceLaundryManager.save(balanceLaundry);
        }

        getSession().setAttribute("laundryRequest", laundryRequest);

        System.out.println("LaundryRequestAction -> end save() ");

        if (!isNew) {
            saveMessage(getText("laundryRequest.updated"));
            return INPUT;
        } else {
            saveMessage(getText("laundryRequest.added"));
            return SUCCESS;
        }
    }

    public String delete() {

//        laundryRequest = laundryRequestManager.get(laundryRequest.getId());
//        List<LaundryRequestItem> laundryRequestItems = laundryRequest.getLaundryRequestItems();
//
//        // return quantities to seam storage
//        for (LaundryRequestItem laundryRequestItem : laundryRequestItems) {
//            //laundryRequestItem = laundryRequestItemManager.get(laundryRequestItem.getId());
//            for (LaundryItem laundryItem : laundryRequestItem.getLaundryItems()) {
//                SeamStorage seamStorage = seamStorageManager.findByPantDesignAndTradeMark(laundryItem.getPantDesign().getId(), laundryRequest.getTradeMark().getId()).get(0);
//                seamStorage.incrementStock(laundryItem.getQuantity());
//                seamStorage = seamStorageManager.save(seamStorage);
//            }
//        }
//
//        laundryRequestManager.remove(laundryRequest.getId());
//
//        getSession().setAttribute("pantDesign", null);
//        getSession().setAttribute("laundryRequest", null);
//        getSession().setAttribute("laundryRequestItem", null);
//
//        saveMessage(getText("laundryRequest.deleted"));
        return "delete";
    }

    private User getUser() {

        HttpServletRequest request = getRequest();
        String userName = request.getRemoteUser();
        List<User> users = userManager.getUsers(new User());

        for (Iterator<User> it = users.iterator(); it.hasNext();) {
            User user = it.next();
            if (user != null) {
                if (user.getUsername().equals(userName)) {
                    System.out.println("user: " + user);
                    return user;
                }
            }
        }
        System.out.println("user null");
        return null;
    }

    public String help() {
        return SUCCESS;
    }

    public String helpList() {
        return SUCCESS;
    }

    public BalanceLaundryManager getBalanceLaundryManager() {
        return balanceLaundryManager;
    }

    public void setBalanceLaundryManager(BalanceLaundryManager balanceLaundryManager) {
        this.balanceLaundryManager = balanceLaundryManager;
    }
   
}

